package com.cssy.yd.logic.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.byd.acegiext.domain.User;
import com.cssy.yd.logic.dao.LogicWhAffDAO;
import com.cssy.yd.logic.dao.LogicWhAffstockDAO;
import com.cssy.yd.logic.entity.LogicWhAffDO;
import com.cssy.yd.logic.entity.LogicWhAffStockFinanceDO;
import com.cssy.yd.logic.entity.LogicWhAffstockDO;
import com.cssy.yd.logic.service.ILogicWhAffService;
import com.cssy.yd.logic.vo.LogicWhAffVO;
import com.cssy.yd.logic.vo.LogicWhAffVO1;
import com.plat.exception.SyBaseException;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.ExcelUtil;

@Service("logicWhAffService")
public class LogicWhAffServiceImpl implements ILogicWhAffService{
	@Resource(name = "logicWhAffDAO")
	private LogicWhAffDAO logicWhAffDAO;
	
	@Resource(name = "logicWhAffstockDAO")
	private LogicWhAffstockDAO logicWhAffstockDAO;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicWhAffDO>  list=logicWhAffDAO.find(scs);
		rs.setRecordCount(logicWhAffDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		Integer i =logicWhAffDAO.deleteLogicWhAffDOByPrimaryKey(id);
		return null;
	}

	@Override
	public Integer update(LogicWhAffDO t, User user) throws SyBaseException {
		t.setDuuser(user.getName());
		t.setDutime(new Date());
		return logicWhAffDAO.updateLogicWhAffDO(t);
	}

	@Override
	public LogicWhAffDO save(LogicWhAffDO t, User user) throws SyBaseException {
		t.setDcruser(user.getName());
		t.setDctime(new Date());
		logicWhAffDAO.insertLogicWhAffDO(t);
		return t;
	}

	@Override
	public LogicWhAffDO findListById(String id) throws SyBaseException {
		return logicWhAffDAO.findLogicWhAffDOByPrimaryKey(id);
	}
	
	/**
	 * 查询出入库明细
	 * @param scs 查询参数
	 * @return
	 * @throws SyBaseException
	 */
	@Override
	public SearchResult findListByExampleForDetails(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicWhAffVO>  list=logicWhAffDAO.findListBySearchConForDetails(scs);
		rs.setRecordCount(logicWhAffDAO.findListBySearchConForDetailsCount(scs));
		rs.setResults(list);
		return rs;
	}
	
	@Override
	public void exportExcel(SearchConditions scs, HttpServletResponse response, String text) throws SyBaseException {
		
		SearchResult result = this.findListByExampleForDetails(scs);
		List<LogicWhAffVO> list = result.getResults();
		for (LogicWhAffVO lwv : list){
			if ("1".equals(lwv.getStatus())){
				lwv.setStatus("一等");
			}
			if ("2".equals(lwv.getStatus())){
				lwv.setStatus("次品");
			}
			if ("3".equals(lwv.getStatus())){
				lwv.setStatus("旧品");
			}
			
			if("OC".equals(lwv.getBillType())){
				lwv.setBillType("其他入库");
			}else if("QC".equals(lwv.getBillType())){
				lwv.setBillType("其他出库");
			}else if("WC".equals(lwv.getBillType())){
				lwv.setBillType("领料出库");
			}else if("WT".equals(lwv.getBillType())){
				lwv.setBillType("领料退料");
			}else if("ST".equals(lwv.getBillType())){
				lwv.setBillType("调拨发起");
			}else if("RC".equals(lwv.getBillType())){
				lwv.setBillType("调拨接收");
			}else if("CR".equals(lwv.getBillType())){
				lwv.setBillType("采购入库");
			}else if("CT".equals(lwv.getBillType())){
				lwv.setBillType("采购退货");
			}else if("PB".equals(lwv.getBillType())){
				lwv.setBillType("库存盘点");
			}
		}
		ExcelUtil.toExcel(list, "单据类型:billType┋单据编码:billCode┋所属区域:area┋物料类型:maType┋物料名称:name┋物料编码:code┋型号:spec┋单位:unit┋仓库:whName┋仓位:hubName┋物品状态:status┋数量:quantity┋建单时间:dctime",text,response);
	}

	@Override
	public SearchResult findAll(Map map) throws SyBaseException {
		SearchResult rs=new SearchResult();
		List<LogicWhAffVO>  list=logicWhAffDAO.findAll(map);
//		if(list.size()>0){
//			for(int i=0;i<list.size();i++){
//				SearchConditions scs = new SearchConditions();
//				scs.addCondition("whcode", list.get(i).getWhCode());
//				scs.addCondition("hubcode", list.get(i).getHubCode());
//				scs.addCondition("code", list.get(i).getCode());
//				List<LogicWhAffstockDO>  list1=logicWhAffstockDAO.find(scs);
//				if(list1.size()>0){
//					list.get(i).setSumquantity(list1.get(0).getYdengquantity()-list.get(i).getRkquantity()-list.get(i).getCkquantity());
//					list.get(i).setJrkc(list1.get(0).getYdengquantity());
//				}
//			}
//		}
		rs.setRecordCount(logicWhAffDAO.countAll(map));
		rs.setResults(list);
		return rs;
	}

	@Override
	public SearchResult findAll1(Map map) throws SyBaseException {
		SearchResult rs=new SearchResult();
		List<LogicWhAffVO1>  list=logicWhAffDAO.findAll1(map);
		rs.setResults(list);
		return rs;
	}

	@Override
	public SearchResult findFinance(Map map) throws SyBaseException {
		SearchResult rs=new SearchResult();
		List<LogicWhAffStockFinanceDO>  list=logicWhAffDAO.findFinance(map);
		rs.setRecordCount(logicWhAffDAO.countFinance(map));
		rs.setResults(list);
		return rs;
	}

	@Override
	public LogicWhAffStockFinanceDO findInOutBycode(Map map)
			throws SyBaseException {
		LogicWhAffStockFinanceDO lwfd = logicWhAffDAO.findInOutBycode(map);
		return lwfd;
	}

	@Override
	public LogicWhAffStockFinanceDO findCKBycode(Map map)
			throws SyBaseException {
		LogicWhAffStockFinanceDO lwfd = logicWhAffDAO.findCKBycode(map);
		return lwfd;
	}
	@Override
	public List<LogicWhAffVO> findStarQty(Map map) throws SyBaseException{
		List<LogicWhAffVO> list = logicWhAffDAO.findStarQty(map);
		return list;
	}
	@Override
	public List<LogicWhAffVO> findEndQty(Map map) throws SyBaseException{
		List<LogicWhAffVO> list = logicWhAffDAO.findEndQty(map);
		return list;
	}
	@Override
	public List<LogicWhAffVO> findInOutQty(Map map) throws SyBaseException{
		List<LogicWhAffVO> list = logicWhAffDAO.findInOutQty(map);
		return list;
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}
	
}
